operators
Types of Operators in JavaScript#
JavaScript has many types of operators and at this workshop we will discuss comparison and logical operators.
Comparison Operators#
Comparison operators are used to compare its operands and evaluate down to a single Boolean value of either True or False.
Comparison operator is also called binary operator because it requires two operands, one before the operator and one after the operator so, we can say that the comparison operator compares its operands and returns a logical value based on whether the comparison is true.The operands can be numerical, string, logical, or object values.
General Syntax#
operand1 operator operand2
Comparison Operators have two types#
1- Strict comparison#
| Operator | Description | Example | Result |
|---|---|---|---|
| === | Equal in value and type | 1 === '1' | false |
| !== | Not equal in value and type | 1 !== '1' | true |
2- Type-converting comparison (or Abstract)#
| Operator | Description | Example | Result |
|---|---|---|---|
| == | Equal to | 1==1 | true |
| != | Not equal to | 1 != 2 | true |
| > | Greater than | 1 > 2 | false |
| < | Less than | 1 < 2 | true |
| >= | Greater than or equal to | 1 >= 1 | true |
| <= | Less than or equal to | 2 <= 1 | false |
Lets discuss them:#
1- A strict comparison is only true if the operands are of the same type and the contents match#
- Identity / strict equality (
===) The identity operator returns true if the operands are strictly equal.
- Non-identity / strict inequality (
!==) The non-identity operator returns true if the operands are not equal and/or not of the same type.
2- The more commonly-used abstract comparison converts the operands to the same type before making the comparison#
- Equality (
==)
- Inequality (
!=)
3- For relational abstract comparisons, the operands are first converted to primitives, then to the same type, before comparison#
Note : Keep this note until you start with JavaScript object session but, just to know If an object (object isn't a primitive type) is compared with a number or string, JavaScript attempts to convert the object to a primitive value, a String or Number value.
Click to read more about comparison operators
Truthy values VS Falsy values#
1. Falsy value : is a value that is considered false when encountered in a Boolean context#
| Falsy values |
|---|
| False |
| Null : (the absence of any value ) |
| NAN : ( not a number) |
| Undefined : ( the primitive value) |
| 0 : (The number zero) |
Empty string : ('',"",``) |
2. Truthy value : All values are truthy unless they are defined as falsy#
Logical Operators#
Logical operators are typically used with Boolean (logical) values to return a Boolean value.However, they actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they will return a non-Boolean value.
There are three logical operators in JavaScript#
1. && (AND)#
The && operation returns the first falsy value or the last value if no falsy value is found.
Example (1) :
Example (2) :
2. || (OR)#
The OR (||) operation returns the first truthy value or the last value if no truthy value is found.
3. ! (Not)#
The not operation (!) returns the inverse boolean value of the variable. It is a unary operation, so it operates on single operand.
!! (Double NOT)#
The single ! converts a value to its truthy or falsely value, which is technically a boolean. But if you need to a real boolean representation of a value for your expression you must convert it to a real boolean value using a double not !!.
Click here for more explanation
Operator Precedence:#
The below operations are listed based on operator precedence. i.e, ! is higher precedence and || is lower precedence.
!(NOT)&&(AND)||(OR)
Click here for more explanation
Conditional Statements#
In JavaScript we have the following conditional statements :
- if
- else
- else if
- switch
Lets discuss them:
1. if statement#
Use if to specify a block of code to be executed, if a specified condition is true.
2. else statement#
Use else to specify a block of code to be executed, if the same condition is false.
what will happened if we wrote greeting = "Good evening"; without else like this?
3. else if statement#
Use else if to specify a new condition to test, if the first condition is false
4. switch statement#
The switch statement executes a block of code depending on different cases. it evaluates an expression.The value of the expression is then compared with the values of each case in the structure. If there is a match, the associated block of code is executed.
Let’s emphasize that the equality check is always strict. The values must be of the same type to match.
Switch features#
- The
switchstatement is often used together with abreakor adefaultkeyword (or both). - The
breakkeyword breaks out of the switch block. This will stop the execution of more execution of code - The
defaultkeyword specifies some code to run if there is no case match.
- If
breakis omitted, the program continues execution at the next case in the switch statement.
- If multiple cases match the provided value, the first case that matches is selected.
- If there is no
breakthen the execution continues with the next cases without any checks.
- Different switch cases share the same code.
When should you use switch instead of if/else statements?#
switch is the perfect solution for long, nested if/else statements (cleaner syntax). read more about this